<?php

/**
 * Author: bluer
 * Date: 6/1/16
 */

require_once('OrderProcess.class.php');

class AsyncOrderProcess extends OrderProcess {

    /**
     * 获取抢购的订单列表
     * @return array|mix
     */
    public function getScrambleOrders() {
        global $cmsdb;

        return $cmsdb->table($cmsdb->orders)
            ->where('success', 1)
            ->where('is_for_scramble', 1)
            ->get();
    }

    /**
     * 更新用户订单的额外信息
     * @param $orderInfo 订单信息. 来源 getScrambleOrders()
     * @return bool
     */
    public function updateExtraInfo($orderInfo) {
        global $cmsdb;

        $this->_setOrderInfo($orderInfo);

        //快递信息
        $resp1 = true;
        if ($orderInfo->has_express) {
            $rupId = $cmsdb->table($cmsdb->userProductR)
                ->where('user_id', $orderInfo->user_id)
                ->where('product_id',$orderInfo->product_id)
                ->where('order_id', $orderInfo->order_id)
                ->value('id');
            if ($rupId) $resp1 = $this->_insertExpress($rupId);
        }

        //邀请码使用记录
        $resp2 = true;
        if ($orderInfo->invite_user_id) {
            $cashbackFee = $cmsdb->table($cmsdb->liveCourses)
                ->where('id', $orderInfo->product_id)
                ->value('cashback_fee');

            if (!empty($cashbackFee)) $resp2 = $this->_insertOrderInvites($cashbackFee);
        }

        $resp3 = $this->enrollEEOCourse($orderInfo->product_id, $orderInfo->user_id);

        if ($resp1 && $resp2 && $resp3) {
            return $cmsdb->table($cmsdb->orders)
                ->where('order_id', $orderInfo->order_id)
                ->update('is_for_scramble', 2);
        }

        return false;
    }
}